package com.DosonLeung.homeschoolcommunication.Utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.widget.ImageView;

import com.DosonLeung.homeschoolcommunication.Constant.UserConstant;
import com.DosonLeung.homeschoolcommunication.R;

import java.io.File;

/**
 * Created by DosonLeung on 2014/10/22.
 */
public class ImageUtil {

    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int s = w*h;
        Bitmap newbmp = bitmap;
        if(s>128000) {
            Matrix matrix = new Matrix();
            float scaleWidth = ((float) width / w);
            float scaleHeight = ((float) height / h);
            matrix.postScale(scaleWidth, scaleHeight);// 利用矩阵进行缩放不会造成内存溢出
            newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        }
        return newbmp;
    }

    public static Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
         return null;
        }
    }


    public static Bitmap adjustBitmapRotation(Bitmap bm, final int orientationDegree){
        Matrix m = new Matrix();
        m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        float targetX, targetY;
        if (orientationDegree == 90) {
            targetX = bm.getHeight();
            targetY = 0;
        } else {
            targetX = bm.getHeight();
            targetY = bm.getWidth();
        }
        final float[] values = new float[9];
        m.getValues(values);
        float x1 = values[Matrix.MTRANS_X];
        float y1 = values[Matrix.MTRANS_Y];
        m.postTranslate(targetX - x1, targetY - y1);
        Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        Canvas canvas = new Canvas(bm1);
        canvas.drawBitmap(bm, m, paint);
        return bm1;
    }

    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    public static Bitmap getBitmapByFilePath(String filePath){
        Bitmap bitmap;
        //Log.e("Album", "Position = "+position + " url = " + url);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        options.inSampleSize = ImageUtil.computeSampleSize(options, -1, 128 * 128);
        options.inJustDecodeBounds = false;
        try {
            bitmap = BitmapFactory.decodeFile(filePath, options);
            return bitmap;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 :(int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

    public static void getMyIcon(ImageView v) {
        Bitmap bitmap;
        final String imagePath = "/" + StorageUtil.getCameraDir().toString() + "/" + UserConstant.USER_AVATAR_PATH;
        File file = new File(imagePath);
        if (file.exists()) {
            //从本地提取
            try {
                bitmap = BitmapFactory.decodeFile(imagePath);
                ((ImageView) v).setImageBitmap(bitmap);
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
        } else {
            //不存在该文件
            ((ImageView) v).setImageResource(R.drawable.icon_loading);
        }
    }

    public static void getMyIcon(ImageView v,String avatarPath) {
        Bitmap bitmap;
        final String imagePath = "/" + StorageUtil.getCameraDir().toString() + "/" + avatarPath;
        File file = new File(imagePath);
        if (file.exists()) {
            //从本地提取
            try {
                bitmap = BitmapFactory.decodeFile(imagePath);
                ((ImageView) v).setImageBitmap(bitmap);
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
        } else {
            //不存在该文件
            ((ImageView) v).setImageResource(R.drawable.icon_loading);
        }
    }
}
